package com.example.bbsbe.controller;

import com.example.bbsbe.entity.profile;
import com.example.bbsbe.entity.user;
import com.example.bbsbe.service.ProfileService;
import com.example.bbsbe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class ProfileController {
    private final ProfileService profileService;

    @Autowired
    public ProfileController(ProfileService profileService) {
        this.profileService = profileService;
    }

    @GetMapping("/api/profiles/")
    public ResponseEntity<?> getprofiles() {
        List<profile> response = profileService.findAll();
        return ResponseEntity.ok(response);
    }

    @PostMapping("/setup/")
    public ResponseEntity<?> createProfile(@RequestBody Map<String, Object> request) {

        String title = (String) request.get("title");
        List<String> imgurl = (List<String>) request.get("imgurl");
        String text = (String) request.get("text");
        String maintext = (String) request.get("maintext");
        String username = (String) request.get("username");
        String type = (String) request.get("type");
        Map<String, Object> response = profileService.createProfile(title, imgurl, text, maintext, username, type);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/getprofile/")
    public ResponseEntity<?> getProfileDetails(@RequestBody Map<String, Long> request) {
        Long proId = request.get("pro_id");
        Long cond = request.get("cond");
        Map<String, Object> response = profileService.getProfileDetails(proId,cond);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/getcheckpro/")
    public ResponseEntity<?> getPendingReviews() {
        List<Map<String, Object>> response = profileService.getPendingReviews();
        return ResponseEntity.ok(response);
    }

    @PostMapping("/getpronum/")
    public ResponseEntity<?> getTotalProfileCount() {
        Map<String, Object> response = profileService.getTotalProfileCount();
        return ResponseEntity.ok(response);
    }

    @PostMapping("/getdiffpronum/")
    public ResponseEntity<?> getProfileCountByType(@RequestBody Map<String, String> request) {
        String proType = request.get("protype");
        Map<String, Object> response = profileService.getProfileCountByType(proType);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/getdiffpro/")
    public ResponseEntity<?> getProfilesByType(@RequestBody Map<String, String> request) {
        String proType = request.get("protype");
        List<Map<String, Object>> response = profileService.getProfilesByType(proType);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/getpro/")
    public ResponseEntity<?> getUserProfiles(@RequestBody Map<String, String> request) {
        String username = request.get("username");
        List<Map<String, Object>> response = profileService.getUserProfiles(username);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/deletepro/")
    public ResponseEntity<?> deleteProfile(@RequestBody Map<String, Long> request) {
        Long proId = request.get("proid");
        profileService.deleteProfile(proId);
        return ResponseEntity.ok(Map.of("condition", 1));
    }

    @PostMapping("/passpro/")
    public ResponseEntity<?> approveProfile(@RequestBody Map<String, Long> request) {
        Long proId = request.get("proid");
        Map<String, Object> response = profileService.approveProfile(proId);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/wrongpro/")
    public ResponseEntity<?> rejectProfile(@RequestBody Map<String, Object> request) {
        Long proId = ((Number) request.get("proid")).longValue();
        String reason = (String) request.get("reason");

        Map<String, Object> response = profileService.rejectProfile(proId, reason);
        return ResponseEntity.ok(response);
    }
}